#include <REG_MPC82G516.H>

unsigned char UART1_RxBuffer[32];
unsigned char UART1_RxHead = 0;
unsigned char UART1_RxTail = 0;
unsigned char UART2_RxBuffer[32];
unsigned char UART2_RxHead = 0;
unsigned char UART2_RxTail = 0;

void UART1_SetBaudrate(unsigned int baudrate)
{
  // Set RS232 baudrate (Speed x1)
  // for Clock 11.0592 MHz
  SCON = 0x52;
  TMOD = 0x20;
  switch(baudrate) {
    case 1200: {
      TH1 = 0xE8;
      break;
    }
    case 2400: {
      TH1 = 0xF4;
      break;
    }
    case 4800: {
      TH1 = 0xFA;
      break;
    }
    case 9600: {
      TH1 = 0xFD;
      break;
    }
    case 19200: {
      TH1 = 0xFD;
      PCON |= 0x80;
      break;
    }
  }
  TR1 = 1;
}

void UART2_SetBaudrate(unsigned int baudrate)
{
  // Set RS232 baudrate (Speed x1)
  // for Clock 11.0592 MHz
  S2CON = 0x52;
  AUXR2 = 0x10;
  switch(baudrate) {
    case 1200: {
      S2BRT = 0xE8;
      break;
    }
    case 2400: {
      S2BRT = 0xF4;
      break;
    }
    case 4800: {
      S2BRT = 0xFA;
      break;
    }
    case 9600: {
      S2BRT = 0xFD;
      break;
    }
    case 19200: {
      S2BRT = 0xFD;
      AUXR2 |= 0x08;
      break;
    }
  }
}

void UART1_Interrupt() interrupt 4
{
  unsigned char Ch;
  if(SCON & 0x01) {
    Ch = SBUF;
    SCON &= 0xFE;
    UART1_RxBuffer[UART1_RxHead++] = Ch;
    UART1_RxHead &= 0x1F;
    if(UART1_RxHead == UART1_RxTail) {
      UART1_RxTail++;
      UART1_RxTail &= 0x1F;
    }
  }
  if(S2CON & 0x01) {
    Ch = S2BUF;
    S2CON &= 0xFE;
    UART2_RxBuffer[UART2_RxHead++] = Ch;
    UART2_RxHead &= 0x1F;
    if(UART2_RxHead == UART2_RxTail) {
      UART2_RxTail++;
      UART2_RxTail &= 0x1F;
    }
  }
}

void UART2_Interrupt() interrupt 12
{
  unsigned char Ch;
  if(SCON & 0x01) {
    Ch = SBUF;
    SCON &= 0xFE;
    UART1_RxBuffer[UART1_RxHead++] = Ch;
    UART1_RxHead &= 0x1F;
    if(UART1_RxHead == UART1_RxTail) {
      UART1_RxTail++;
      UART1_RxTail &= 0x1F;
    }
  }
  if(S2CON & 0x01) {
    Ch = S2BUF;
    S2CON &= 0xFE;
    UART2_RxBuffer[UART2_RxHead++] = Ch;
    UART2_RxHead &= 0x1F;
    if(UART2_RxHead == UART2_RxTail) {
      UART2_RxTail++;
      UART2_RxTail &= 0x1F;
    }
  }
}

void UART1_TxByte(unsigned char x)
{
  TI = 0;
  SBUF = x;
  TI = 0;
  while(!TI);
}

void UART2_TxByte(unsigned char x)
{
  S2CON &= 0xFD;
  S2BUF = x;
  S2CON &= 0xFD;
  while(!(S2CON & 0x02));
}
/*
void UART1_TxStr(unsigned char *Text)
{
  while(*Text) {
    UART1_TxByte(*Text);
    Text++;
  }
}

void UART2_TxStr(unsigned char *Text)
{
  while(*Text) {
    UART2_TxByte(*Text);
    Text++;
  }
}

unsigned char UART1_RxByte(void)
{
  while((SCON & 0x01) == 0);
  SCON &= 0xFE;
  return SBUF;
}

unsigned char UART2_RxByte(void)
{
  while((S2CON & 0x01) == 0);
  S2CON &= 0xFE;
  return S2BUF;
}
*/
void UART1_Init(unsigned int Baudrate, char UseIntr)
{
  UART1_SetBaudrate(Baudrate);
  // Use Interrupt No.4
  SCON &= 0xFC;
  if(UseIntr) {
    ES = 1;
    EA = 1;
  }
}

void UART2_Init(unsigned int Baudrate, char UseIntr)
{
  UART2_SetBaudrate(Baudrate);
  // Use Interrupt No.12
  S2CON &= 0xFC;
  if(UseIntr) {
    AUXIE |= (1<<4);
    EA = 1;
  }
}

